﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Fin;
using ESOL.WinUI.MasterData;
using ESOL.Business.Mst;
using ESOL.WinUI.Sales;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Finance
{
    public partial class frmMaintainARAccountDetail : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        private MstCustomerEntity mSelectedCustomer;
        private FinCustomerAccountEntity mSelectedCustomerAccount;
        #endregion

        #region Contructor
        public frmMaintainARAccountDetail()
        {
            InitializeComponent();
        }
        public frmMaintainARAccountDetail(FinCustomerAccountEntity account)
        {
            InitializeComponent();
            mSelectedCustomerAccount = account;
            if (account != null)
            {
                mSelectedCustomer = MstCustomerBs.GetMstCustomerByID(account.CustomerId);
                if (mSelectedCustomer != null)
                {
                    txtCustomer.Text = mSelectedCustomer.CustomerName;
                }
            }
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = "";
            if (mSelectedCustomer != null)
            {
                condition += " AND CAI.CUSTOMER_ID = '" + mSelectedCustomer.CustomerId + "' ";
            }
            if (txtAmountPerson.Tag != null && txtAmountPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtAmountPerson.Tag;
                condition += " AND CAI.AMOUNT_PERSON = '" + entity.PersonId + "' ";
            }
            if (dtpFromDate.EditValue!=null)
            {
                condition += " AND CAI.AMOUNT_DATE >= CONVERT(DATETIME,'" + dtpFromDate.EditValue.ToString() + "')";
            }
            if (dtpEndDate.EditValue != null)
            {
                condition += " AND CAI.AMOUNT_DATE <= CONVERT(DATETIME,'" + dtpEndDate.EditValue.ToString() + "')";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = "";
            string orderBy = "CAI.AMOUNT_DATE DESC";
            //DataTable dt = FinCustomerAccountItemBs.GetByCondition(condition, orderBy, rowCount);
            DataSet ds = new DataSet();
            DataTable dtMaster = FinCustomerAccountItemBs.GetTotalAmountByCondition(condition, orderBy, rowCount);
            DataTable dtDetail = new DataTable();
            dtDetail.Columns.Add("CUSTOMER_ACCOUNT_ITEM_ID");
            dtDetail.Columns.Add("APPLICATION_ID");
            dtDetail.Columns.Add("RELATED_ORDER_CODE");
            dtDetail.Columns.Add("SALES_ORDER_AMOUNT", typeof(decimal));
            dtDetail.Columns.Add("BILLING_AMOUNT",typeof(decimal));
            dtDetail.Columns.Add("RECEIVED_AMOUNT", typeof(decimal));
            dtDetail.Columns.Add("RECEIVABLE_AMOUNT", typeof(decimal));
            dtDetail.Columns.Add("DELIVERY_AMOUNT_NO_BILLING", typeof(decimal));
            dtDetail.Columns.Add("DELIVERY_AMOUNT_BILLED", typeof(decimal));
            dtDetail.Columns.Add("SALES_AMOUNT", typeof(decimal));
            ds.Tables.Add(dtMaster.Copy());
            ds.Tables.Add(dtDetail);
            ds.Relations.Add(new DataRelation("MasterDetail", ds.Tables[0].Columns["APPLICATION_ID"], ds.Tables[1].Columns["APPLICATION_ID"]));
            devDataGridView1.DataSource = ds.Tables[0];
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindDataGrid();
            base.OnShown(e);
            dtpFromDate.Enabled = true;
            dtpEndDate.Enabled = true;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        private void txtCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainCustomerQuery frm = new frmMaintainCustomerQuery();
            frm.ShowAllCustomer = true;
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedCustomer != null)
            {
                txtCustomer.Text = frm.SelectedCustomer.CustomerName;
                mSelectedCustomer = frm.SelectedCustomer;
            }
            else
            {
                txtCustomer.Text = "";
                mSelectedCustomer = null;
            }
        }

        private void txtAmountPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtAmountPerson.Text = frm.SelectedPerson.NameCn;
                txtAmountPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtAmountPerson.Text = "";
                txtAmountPerson.Tag = null;
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            string sApplicationID = gridView1.GetDataRow(e.RowHandle)["APPLICATION_ID"].ToString();
            if (string.IsNullOrEmpty(sApplicationID) == true)
            {
                return;
            }
            string sCondition = " and CAI.APPLICATION_ID='"+sApplicationID+"'";
            DataTable dt = FinCustomerAccountItemBs.GetByCondition(sCondition, "", "");
            DataTable dtMaster = devDataGridView1.DataSource as DataTable;
            DataSet dsSource = dtMaster.DataSet;
            DataTable dtDetail = dsSource.Tables[1];
            foreach (DataRow dr in dt.Rows)
            {
                if (dtDetail.Select("CUSTOMER_ACCOUNT_ITEM_ID='" + dr["CUSTOMER_ACCOUNT_ITEM_ID"] + "'").Length == 0)
                {
                    DataRow drDetailNew = dtDetail.NewRow();
                    for (int i = 0; i < dtDetail.Columns.Count; i++)
                    {
                        drDetailNew[i] = dr[dtDetail.Columns[i].ColumnName];
                    }
                    dtDetail.Rows.Add(drDetailNew);
                }
            }
        }

        private void gridView1_MasterRowExpanded(object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e)
        {
             DevExpress.XtraGrid.Views.Grid.GridView aView = (DevExpress.XtraGrid.Views.Grid.GridView)gridView1.GetDetailView(e.RowHandle, 0);
             if (aView != null)
             {
                 aView.BestFitColumns();
                 aView.Name = "金额明细";
                 aView.Columns["RELATED_ORDER_CODE"].Caption = "相关单据";
                 aView.Columns["RELATED_ORDER_CODE"].OptionsColumn.ReadOnly = true;
                 aView.Columns["SALES_ORDER_AMOUNT"].Caption = "计划金额";
                 aView.Columns["BILLING_AMOUNT"].Caption = "开票金额";
                 aView.Columns["RECEIVED_AMOUNT"].Caption = "已到金额";
                 aView.Columns["RECEIVABLE_AMOUNT"].Caption = "应收账款";
                 aView.Columns["DELIVERY_AMOUNT_NO_BILLING"].Caption = "已交货金额";
                 aView.Columns["DELIVERY_AMOUNT_BILLED"].Caption = "交货未开票金额";
                 aView.Columns["SALES_AMOUNT"].Caption = "销售费用";
                 if (aView.Columns.ColumnByFieldName("CUSTOMER_ACCOUNT_ITEM_ID") != null)
                 {
                     aView.Columns["CUSTOMER_ACCOUNT_ITEM_ID"].VisibleIndex = -1;
                 }
                 if (aView.Columns.ColumnByFieldName("APPLICATION_ID") != null)
                 {
                     aView.Columns["APPLICATION_ID"].VisibleIndex = -1;
                 }
             }
        }

                #endregion
    }
}
